package whf.framework.security.web;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Map;

import whf.framework.entity.AbstractEntity;
import whf.framework.meta.service.ObjectServiceImp;
import whf.framework.security.entity.Permission;
import whf.framework.security.service.PermissionService;
import whf.framework.security.service.PermissionServiceImp;
import whf.framework.util.BeanUtils;
import whf.framework.util.StringUtils;
import whf.framework.util.Utils;
import whf.framework.web.struts.WebData;
import whf.framework.web.struts.WebDispatchAction;
import whf.framework.web.struts.WebForward;

/**
 * @author wanghaifeng
 * @email king@126.com
 * @modify 2006-05-31
 */
public class PermissionAction extends WebDispatchAction {

	@Override
	public WebForward list(AbstractEntity webEntity, WebData data) throws Exception {
		String boClassName = ((Permission)webEntity).getTarget();
		whf.framework.meta.entity.Object obj = ObjectServiceImp.getObjectService().findByEntityClass(boClassName);
		PermissionService service = PermissionServiceImp.getPermissionService();
		List<Permission> permissions = service.findByTarget(obj.getBoClassName());
		data.setAttribute("Object", obj);
		data.setAttribute("permissions", permissions);
		return data.forward("list");
	}

	public WebForward viewByTarget(AbstractEntity webEntity, WebData data) throws Exception {
		String boClassName = ((Permission)webEntity).getTarget();
		whf.framework.meta.entity.Object obj = ObjectServiceImp.getObjectService().findByEntityClass(boClassName);
		PermissionService service = PermissionServiceImp.getPermissionService();
		List<Permission> permissions = service.findByTarget(obj.getBoClassName());
		if(obj != null){
			Class actionClass = Utils.getClassByClassName(obj.getActionClassName());
			Map<String, Method> methods = Utils.newHashMap();
			for(int i=0; i<METHOD_PARAMETER_TYPES.length; i++){
				methods.putAll(BeanUtils.getMethodsByParamTypes(actionClass, METHOD_PARAMETER_TYPES));
			}
			Map<String, String> operations = super.getOperationNames();
			for(String methodName: methods.keySet()){
				Method operation = null;
				Method prepareOperation = null;
				if(methodName.startsWith("prepare")){
					prepareOperation = methods.get(methodName);
					operation = methods.get(methodName.substring(7));
				} else {
					operation = methods.get(methodName);
					prepareOperation = methods.get("prepare"+StringUtils.capitalize(methodName));
				}
				if(operation != null && operation.getModifiers() != Modifier.PUBLIC){
					continue;
				}
				if(prepareOperation != null && prepareOperation.getModifiers() != Modifier.PUBLIC){
					continue;
				}
				boolean found = false;
				for(Permission p: permissions){
					if(StringUtils.equals(p.getOperation(), methodName) || StringUtils.equals(p.getPrepareOperation(), methodName)){ //存在对应的方法记录
						boolean changed = false;
						if(prepareOperation != null && !StringUtils.equals(p.getPrepareOperation(), prepareOperation.getName())){
							p.setPrepareOperation(prepareOperation.getName());
							changed = true;
						}
						if(operation != null && !StringUtils.equals(p.getOperation(), operation.getName())){
							p.setOperation(operation.getName());
							changed = true;
						}
						if(changed){
							service.update(p);
						}
						found = true;
					}
				}
				if(!found){
					Permission perm = new Permission();
					perm.setTarget(boClassName);
					if(operation != null){
						perm.setOperation(operation.getName());
						perm.setName(operations.get(operation.getName()));
					}
					if(prepareOperation != null){
						perm.setPrepareOperation(prepareOperation.getName());
						if(StringUtils.isEmpty(perm.getName())){
							perm.setName(operations.get(prepareOperation.getName()));
						}
					}
					if(StringUtils.isEmpty(perm.getName())){
						if(!StringUtils.isEmpty(perm.getOperation())){
							perm.setName(perm.getOperation());
						} else if(!StringUtils.isEmpty(perm.getPrepareOperation())){
							perm.setName(perm.getPrepareOperation());
						}
					}
					service.create(perm);
					permissions.add(perm);
				}
			}
		}
		data.setAttribute("Object", obj);
		data.setAttribute("permissions", permissions);
		return data.forward("list");
	}
} 